package cn.iocoder.yudao.module.blog.controller.admin.improvesection;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.improvesection.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvesection.BlogImproveSectionDO;
import cn.iocoder.yudao.module.blog.convert.improvesection.BlogImproveSectionConvert;
import cn.iocoder.yudao.module.blog.service.improvesection.BlogImproveSectionService;

@Tag(name = "管理后台 - 课程章节")
@RestController
@RequestMapping("/blog/improve-section")
@Validated
public class BlogImproveSectionController {

    @Resource
    private BlogImproveSectionService improveSectionService;

    @PostMapping("/create")
    @Operation(summary = "创建课程章节")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:create')")
    public CommonResult<Long> createImproveSection(@Valid @RequestBody BlogImproveSectionCreateReqVO createReqVO) {
        return success(improveSectionService.createImproveSection(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程章节")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:update')")
    public CommonResult<Boolean> updateImproveSection(@Valid @RequestBody BlogImproveSectionUpdateReqVO updateReqVO) {
        improveSectionService.updateImproveSection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程章节")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:improve-section:delete')")
    public CommonResult<Boolean> deleteImproveSection(@RequestParam("id") Long id) {
        improveSectionService.deleteImproveSection(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程章节")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:query')")
    public CommonResult<BlogImproveSectionRespVO> getImproveSection(@RequestParam("id") Long id) {
        BlogImproveSectionDO improveSection = improveSectionService.getImproveSection(id);
        return success(BlogImproveSectionConvert.INSTANCE.convert(improveSection));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程章节列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:query')")
    public CommonResult<List<BlogImproveSectionRespVO>> getImproveSectionList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogImproveSectionDO> list = improveSectionService.getImproveSectionList(ids);
        return success(BlogImproveSectionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程章节分页")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:query')")
    public CommonResult<PageResult<BlogImproveSectionRespVO>> getImproveSectionPage(@Valid BlogImproveSectionPageReqVO pageVO) {
        PageResult<BlogImproveSectionDO> pageResult = improveSectionService.getImproveSectionPage(pageVO);
        return success(BlogImproveSectionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程章节 Excel")
    @PreAuthorize("@ss.hasPermission('blog:improve-section:export')")
    @OperateLog(type = EXPORT)
    public void exportImproveSectionExcel(@Valid BlogImproveSectionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogImproveSectionDO> list = improveSectionService.getImproveSectionList(exportReqVO);
        // 导出 Excel
        List<BlogImproveSectionExcelVO> datas = BlogImproveSectionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "课程章节.xls", "数据", BlogImproveSectionExcelVO.class, datas);
    }

}
